package com.qiguliuxing.dts.db.service;

import com.github.pagehelper.PageHelper;
import com.qiguliuxing.dts.db.dao.DtsAdminMapper;
import com.qiguliuxing.dts.db.domain.DtsAdmin;
import com.qiguliuxing.dts.db.domain.DtsAdminExample;
import com.qiguliuxing.dts.db.domain.DtsAdmin.Column;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsAdminService {
	private final Column[] result = new Column[] { Column.id, Column.username, Column.avatar, Column.roleIds,Column.nickName,Column.status,Column.shareUrl,Column.wxOpenId,Column.receiveMsgStatus,Column.isLockCustomer,Column.belongDepartment,Column.belongHospital };
	@Resource
	private DtsAdminMapper adminMapper;

	public List<DtsAdmin> findAdmin(String username) {
		DtsAdminExample example = new DtsAdminExample();
		example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
		return adminMapper.selectByExample(example);
	}

	public DtsAdmin findAdmin(Integer id) {
		return adminMapper.selectByPrimaryKey(id);
	}

	public List<DtsAdmin> querySelective(String username, String nickName, String belongHospital, String belongDepartment, String type, Integer page, Integer limit, String sort, String order) {
		DtsAdminExample example = new DtsAdminExample();
		DtsAdminExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(username)) {
			criteria.andUsernameLike("%" + username + "%");
		}

		if (!StringUtils.isEmpty(nickName)) {
			criteria.andNickNameLike("%" + nickName + "%");
		}

		if (!StringUtils.isEmpty(belongHospital)) {
			criteria.andBelongHospitalLike("%" + belongHospital + "%");
		}

		if (!StringUtils.isEmpty(belongDepartment)) {
			criteria.andBelongDepartmentLike("%" + belongDepartment + "%");
		}

		criteria.andDeletedEqualTo(false);

		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}

		if (!StringUtils.isEmpty(type)) {
			criteria.andTypeEqualToColumn(type);
		}
		PageHelper.startPage(page, limit);
		return adminMapper.selectByExampleSelective(example, result);
	}

	public int updateById(DtsAdmin admin) {
		admin.setUpdateTime(LocalDateTime.now());
		return adminMapper.updateByPrimaryKeySelective(admin);
	}

	public void deleteById(Integer id) {
		adminMapper.logicalDeleteByPrimaryKey(id);
	}

	public void add(DtsAdmin admin) {
		admin.setAddTime(LocalDateTime.now());
		admin.setUpdateTime(LocalDateTime.now());
		adminMapper.insertSelective(admin);
	}

	public DtsAdmin findById(Integer id) {
		return adminMapper.selectByPrimaryKeySelective(id, result);
	}

	public List<DtsAdmin> allAdmin() {
		DtsAdminExample example = new DtsAdminExample();
		example.or().andDeletedEqualTo(false);
		return adminMapper.selectByExample(example);
	}

	public List<DtsAdmin> receiveAdmin() {
		DtsAdminExample example = new DtsAdminExample();
		example.or().andReceiveMsgStatusEqualTo(true).andTypeEqualToColumn("0").andDeletedEqualTo(false);
		return adminMapper.selectByExample(example);
	}

	public DtsAdmin findAdminByWxOpenId(String wxOpenId) {
		return adminMapper.selectByWxOpenId(wxOpenId);
	}

	public DtsAdmin findLastDtsAdmin() {
		return adminMapper.selectLastDtsAdmin();
	}

	/**
	 * 绑定微信公众号openId
	 * @param admin
	 * @return
	 */
	public int bingAdminWxOpenId(DtsAdmin admin) {
		List<DtsAdmin> adminList = findAdmin(admin.getUsername());
		if (adminList.size() == 1) {
			DtsAdmin dtsAdmin = adminList.get(0);
			dtsAdmin.setWxOpenId(admin.getWxOpenId());
			dtsAdmin.setTel(admin.getTel());
			return updateById(dtsAdmin);
		}
		return 0;
	}

	/**
	 * 解除绑定微信公众号openId
	 * @param wxOpenId
	 * @return
	 */
	public int relieveAdminUserByWxOpenId(String wxOpenId) {
		DtsAdmin adminByWxOpenId = findAdminByWxOpenId(wxOpenId);
		if(!ObjectUtils.isEmpty(adminByWxOpenId)){
			adminByWxOpenId.setWxOpenId("-");
			adminByWxOpenId.setUpdateTime(LocalDateTime.now());
			return updateById(adminByWxOpenId);
		}
		return 0;
	}
}
